package cn.wolfcode.crm.web.controller;
import cn.wolfcode.crm.domain.Latentclientplan;
import cn.wolfcode.crm.query.LatentClientPlanQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ILatentClientPlaService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("latentclientplan")
public class LatentClientPlanController {

    @Autowired
    private ILatentClientPlaService planService;

    //创建无参构造器
    public LatentClientPlanController() {
    }

    @RequestMapping({"view"})
    @RequiresPermissions(value = {"plan:view", "客户开发计划页面"}, logical = Logical.OR)
    public String view() {
        //返回到客户开发计划界面
        return "/latentclientplan/list";
    }

    @RequestMapping({"query"})
    @ResponseBody
    @RequiresPermissions(
            value = {"plan:view", "客户开发计划查看"},
            logical = Logical.OR
    )
    //查询所有数据
    public PageResult query(LatentClientPlanQueryObject qo) {

        return this.planService.query(qo);
    }

    @RequestMapping({"saveOrUpdate"})
    @ResponseBody
    //判断是保存还是修改操作
    public JsonResult saveOrUpdate(Latentclientplan entity) {
        JsonResult result = new JsonResult();

        try {
            if (entity.getId() == null) {
                //保存数据
                this.planService.insert(entity);
            } else {
                //修改数据
                this.planService.updateByPrimaryKey(entity);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("保存失败");
        }
    }

    @RequestMapping({"selectAll"})
    @ResponseBody
    @RequiresPermissions(
            value = {"selectAll:view", "客户开发计划数据"},
            logical = Logical.OR
    )
    public Object selectAll() {
        //查询客户开发计划数据
        return this.planService.selectAll();
    }

    @RequestMapping({"changeResource"})
    @ResponseBody
    @RequiresPermissions(value = {"plan:changResource", "资源池客户查询"},logical = Logical.OR)
    //改变资源方法
    public JsonResult changeResource(Long id) {
        JsonResult result = new JsonResult();

        try {
            //改变资源
            this.planService.changeResource(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("保存失败");
        }
    }

    @RequestMapping({"changeState"})
    @ResponseBody
    @RequiresPermissions(value = {"plan:changeState", "正式客户查询"}, logical = Logical.OR)
    //改变状态方法
    public JsonResult changeState(Long id) {
        JsonResult result = new JsonResult();

        try {
            //改变状态
            this.planService.changeState(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("保存失败");
        }
    }
}
